<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    /**
     * Main class for working with seo descriptions.
     */
    class seo extends CI_Controller {

        private $data;

        function __construct() {

            parent::__construct();

            if (!$this->session->userdata('logged_in'))
                redirect(base_url('login'));

            $this->load->config('languages');
            $this->load->model('posts_model');
            $this->load->model('seo_model');

            $this->translate = & $this->factory->create('translate', 'foo', 'bar');
            $this->activeLang = $this->session->userdata('my_language');
        }

        public function index() {
            if (!isset($_GET['post_type']))
                $_GET['post_type'] = "page";

            $this->view_all();
        }

        /**
         * @param $_GET['post_type']
         */
        function view_all() {
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = 'View all ' . $_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type'];

            $posts = $this->posts_model->view_all('page');
            $this->data['all_posts'] = $this->translate->res_array($posts, $this->activeLang);

            $this->template->load('seo/view_all', $this->data);
        }

        /**
         * 
         * @param $_GET['post_ID']
         * @param $_GET['post_type']
         */
        function view_edit() {
            $this->data['post_action'] = $_GET['post_action'];
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = 'View ' . $_GET['post_type'];
            $this->data['post'] = $this->posts_model->view_edit($_GET['post_ID']);

            $this->data['post']['post_title'] = $this->strparse->find_lang_in_string($this->data['post']['post_title'], $this->activeLang);

            $this->data['seo'] = $this->seo_model->find_by_id($_GET['post_ID']);

//            $this->firephp->log($this->data);
            $this->template->load('seo/edit_seo', $this->data);
        }

        /**
         * Submit Seo Info
         *
         * Add/Edit Seo Info
         *
         */
        public function submit() {

            $_POST = $this->translate->parse_asoc($_POST);

            $SEO_data = array(
                'title' => $_POST['title'],
                'description' => $_POST['description'],
                'keywords' => $_POST['keywords'],
                'posts_ID' => $_POST['post_ID']
            );

            $SEO = $this->seo_model->find_by_id($_POST['post_ID']);

            if (empty($SEO)):
                $this->seo_model->insert($SEO_data);
            else:
                $this->seo_model->update($SEO['ID'], $SEO_data);
            endif;

            echo json_encode(array('action' => TRUE, 'msg' => 'Successful operation.'));
        }

    }

?>
